package other.xsy.passlys.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.http.CommonData;
import com.rkhd.platform.sdk.http.CommonHttpClient;
import com.rkhd.platform.sdk.http.HttpResult;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.QueryResult;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Description: 使用管理员的账号信息进行操作
 * @Author: 黎炎松
 * @Date: 2024/6/28 10:37
 */
public class CustomAdminUtil {
    private static CommonHttpClient client = new CommonHttpClient();

    private static String userName = "crm.gc@getech.cn";
    private static String password = "zxcvbnm,./";
    private static String securityCode = "u4mNNaPg";
    private static String clientId = "48d0cc503b721322e4e618b87b9f81a8";
    private static String clientSecret = "ea1a5bd9e522965a5d829c867ac8a9b8";
    private static String domain = "https://api-p05.xiaoshouyi.com";
    private static String getTokenOauth = "https://login.xiaoshouyi.com/auc/oauth2/token";

    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "使用管理员角色操作数据 ==> ";


    /**
     * 发送请求
     * @param sql
     */
    public static QueryResult<JSONObject> sendAdminXoqlQuery(String sql) {
        QueryResult<JSONObject> queryResult = new QueryResult();
        queryResult.setSuccess(false);

        String reqUrl = domain + "/rest/data/v2.0/query/xoql";
        String adminToken = getAdminToken();
        if (StringUtils.isBlank(adminToken)) {
            logger.debug(LOGGER_MSG + "获取token异常");
            return queryResult;
        }

        CommonData commonData = new CommonData();
        commonData.setCall_type("POST");
        commonData.setCallString(reqUrl);
        commonData.addHeader("Content-Type", "application/x-www-form-urlencoded");
        commonData.addHeader("Authorization", "Bearer " + adminToken);
        commonData.putFormData("xoql", sql);


        HttpResult result = client.execute(commonData);
        if (result != null && StringUtils.isNotBlank(result.getResult())) {
            JSONObject jsonObject = JSONObject.parseObject(result.getResult());
            Long code = jsonObject.getLong("code");
            if (code != 200) {
                String msg = jsonObject.getString("msg");
                queryResult.setSuccess(false);
                queryResult.setErrorMessage(msg);
                return queryResult;
            }

            JSONObject resultJsonObject = jsonObject.getJSONObject("data");
            queryResult.setSuccess(true);
            queryResult.setTotalCount(resultJsonObject.getLong("totalSize"));
            queryResult.setCurrentCount(resultJsonObject.getLong("count"));
            JSONArray jsonArray = resultJsonObject.getJSONArray("records");
            List<JSONObject> records = new ArrayList();
            Iterator var11 = jsonArray.iterator();

            while(var11.hasNext()) {
                Object obj = var11.next();
                Map map = (Map)obj;
                JSONObject data = new JSONObject(map);
                records.add(data);
            }
            queryResult.setRecords(records);
        }
        return queryResult;
    }

    /**
     * 获取token
     * @return
     */
    private static String getAdminToken() {
        String adminToken = "";
        String oauthUrl = getTokenOauth + "?grant_type=password" + "&client_id=" + clientId + "&client_secret=" + clientSecret + "&username=" + userName + "&password=" + password + securityCode;
        CommonData commonData = new CommonData();
        commonData.setCall_type("GET");
        commonData.setCallString(oauthUrl);
        HttpResult result = client.execute(commonData);
        if (result != null && StringUtils.isNotBlank(result.getResult())) {
            JSONObject jsonObject = JSONObject.parseObject(result.getResult());
            if (jsonObject.containsKey("access_token")) {
                adminToken = jsonObject.getString("access_token");
            }
        }
        logger.debug(LOGGER_MSG + " adminToken:" + adminToken);
        return adminToken;
    }
}
